// miniprogram/pages/my/myAddress.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userAddress: [],
        selectIndex: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let _this = this;
        wx.getStorage({
            key: 'userAddress',
            success: function(res) {
                if (res.data && res.data.length > 0) {
                    console.log(res.data)
                    _this.setData({
                        userAddress: res.data
                    })
                }
            },
            fail: (err) => {
                wx.showModal({
                    title: '提示',
                    content: '您还没有备用的收货地址',
                })
            }
        })
    },
    radioChange: function(e) {
        var userAddress = this.data.userAddress;
        for (var i = 0, len = userAddress.length; i < len; ++i) {
            if (i == e.detail.value) {
                userAddress[i].checked = i == e.detail.value;
                this.setData({
                    selectIndex: i
                })
            }
            userAddress[i].checked = i == e.detail.value
        }
        this.setData({
            userAddress: userAddress
        });
    },
    editAddress() {
        wx.navigateTo({
            url: `/pages/my/addAddress?addressIndex=${this.data.selectIndex}`
        })

    },
    deleteAddress() {
        let index = this.data.selectIndex;
        console.log(index);
        let address = wx.getStorageSync('userAddress');
        address.splice(index, 1);
        this.setData({
            userAddress: address
        })
        wx.setStorageSync('userAddress', address)
    },
})